//https://www.bilibili.com/video/BV1d54y1f7B3/?spm_id_from=333.337.search-card.all.click&vd_source=d6fe65f095f01f05591e89d810720ccc
// 2024-10-20

import java.util.Arrays;

public class P020_子数组的最大平均数_滑动窗口 {

    public static void main(String[] args) {
        int[] nums = {1,3,5,7,9,11};
        System.out.println(fun(nums));
    }

    private static double fun(int[] nums) {
        double maxavg = Integer.MIN_VALUE;
        for (int i = 1;i <= nums.length;i++) {
            double tempavg = fun(nums,i);
            if (tempavg > maxavg) maxavg = tempavg;
        }
        return maxavg;
    }

    private static double fun(int[] nums,int k) {//k指的是窗口大小
        int sum = 0;

        for (int i = 0; i < k; i++) sum += nums[i];//第一个窗口求和

        double maxavg = (double)sum / k;

        for (int i = k;i < nums.length;i++) {
            //移动，加上新加入的值和减去出去的值
            sum = sum - nums[i-k] + nums[i];
            double tempavg = (double)sum / k;
            if (tempavg > maxavg) maxavg = tempavg;
        }
        return maxavg;
    }
}
